package com.dundun.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dundun.common.utils.Ajax;
import com.dundun.service.ResumeService;
import com.dundun.domain.Resume;
import com.dundun.mapper.ResumeMapper;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

/**
 * @author wq
 * @description 针对表【resume】的数据库操作Service实现
 * @createDate 2022-11-19 10:02:05
 */
@Service("resumeService")
public class ResumeServiceImpl extends ServiceImpl<ResumeMapper, Resume>
        implements ResumeService {
    @Resource
    private ResumeMapper resumeMapper;
    @Value("${server.port}")
    private String port;

    @Value("${server.servlet.context-path}")
    private String contextPath;

    // 图片请求路径
    private String requestUrl = "http://localhost:";
    // 用户空间下的地址
    private final String saveFilePath = System.getProperty("user.dir") + File.separator + "files";


    @Override
    public int addResume(MultipartFile multipartFile, int userId,String userFileName) {

        requestUrl = requestUrl + port + contextPath + "/";

        System.out.println("impl:" + requestUrl);
        String originalFilename = multipartFile.getOriginalFilename();
        System.out.println(originalFilename);
        String ext = originalFilename.substring(originalFilename.lastIndexOf(".")); //.jpg
        String fileName = UUID.randomUUID() + ext;

        String savaPath = saveFilePath + File.separator + fileName;
        System.out.println(savaPath);
        File file = new File(savaPath);
        if (!file.getParentFile().exists()) {
            file.getParentFile().mkdirs();
        }
        if (file.exists()) {
            return 0;
        }
        // 保存文件
        try {
            multipartFile.transferTo(file);
            Integer integer = new Integer(userId);
            long userid = integer.longValue();
            Resume resume = new Resume();
            resume.setUserId(userid);
            resume.setResumeFormat(userFileName);
            resume.setResumeUrl(requestUrl + fileName);
            resume.setResumeName(fileName);
            resumeMapper.insert(resume);
            return 1;
        } catch (IOException e) {
            e.printStackTrace();
            return -1;
        }

    }
}




